/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fait.ferm.ui;

import fait.ferm.ui.tables.FeedPersonalRow;
import com.sai.javafx.calendar.FXCalendar;
import com.sai.javafx.calendar.FXCalendarUtility;
import fait.ferm.global.Glob;
import fait.ferm.persists.Cow;
import fait.ferm.persists.Directory;
import fait.ferm.persists.SimpleDirItem;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;

/**
 *
 * @author alex
 */
public class PlanTabFeedingDialogPaneController implements Initializable{
	
	public ObjectProperty<FeedPersonalRow> returnedValue;
	
	private ObjectProperty<Cow> selectedCow;
	private Boolean isGroup;
	private FXCalendar calendarBeg;	
	private FXCalendar calendarEnd;	

	
	@FXML
    private BorderPane dialogWindow;
	@FXML
    private Label cowNameLb;
	@FXML
    private TextField quantityTf;
	@FXML
    private Button cowSelectBtn;
	@FXML
    private ComboBox supCombo;
	@FXML
    private CheckBox isGroupCb;
	@FXML
	private GridPane grid;

	
	

	@Override
	public void initialize(URL url, ResourceBundle rb) {
		decorate();
		selectedCow = new SimpleObjectProperty<>();
		addListeners();
		fillSuppliersCombo();
		addCalendar();
		fillControls();

	}

	private void fillControls(){
		if( PlanTabFeedingPaneController.IS_NEW) return;
		FeedPersonalRow r = PlanTabFeedingPaneController.CUR_PERS_ROW;

		selectedCow.set( r.cowProperty().get());
		cowNameLb.setText(selectedCow.get().toString());
		isGroupCb.setSelected(r.isGroup());
		isGroup = r.isGroup();
		quantityTf.setText(String.format("%.0f", r.getQuantity()));
		supCombo.getSelectionModel().select(r.getSup());
		calendarBeg.setValue(java.sql.Date.valueOf( r.getBegDate()));
		calendarEnd.setValue(java.sql.Date.valueOf( r.getEndDate()));
	}
	
	@FXML
    private void dialogWindowCloseClick() {
        //TODO
        dialogWindow.getChildren().clear();
		Glob.dialogPane.toBack();
    }

	private void decorate(){
		Image im = new Image(getClass().getResourceAsStream("res/select.png"));
		cowSelectBtn.setGraphic(new ImageView(im));
//		im = new Image(getClass().getResourceAsStream("res/calendar.png"));
		cowNameLb.setStyle("-fx-border-width: 1px 1px 1px 1px;-fx-border-color: #8f8f8f;");
	}
	
	@FXML
	private void selectCow(){
	try {
		FXMLLoader fl = new FXMLLoader();
		StackPane p = (StackPane) fl.load(getClass().getResource("CowSelectDialog.fxml").openStream());
		CowSelectDialogController c = fl.getController();
		c.dialogWindow = Glob.dialogPane2;
		c.returnedValue = selectedCow;
		Glob.dialogPane2.centerProperty().set(p);
		Glob.dialogPane2.toFront();
	} catch (IOException ex) {
		Logger.getLogger(CreditTabController.class.getName()).log(Level.SEVERE, null, ex);
	}		
	}
	
	private void addListeners(){
		//изменение выбранной коровы
		selectedCow.addListener(new ChangeListener<Cow>(){
			@Override
			public void changed(ObservableValue<? extends Cow> ov, Cow t, Cow t1) {
				if(t1!=null)
					cowNameLb.textProperty().set(t1.toString());
				else
					cowNameLb.textProperty().set("");
			}		
		});
		
	}
	
	private void fillSuppliersCombo(){
		Directory sd = Glob.getDirInCache("supplies");
		ObservableList<SimpleDirItem> ar = FXCollections.observableArrayList(sd.getItems());
		supCombo.setItems(ar);
	}
	
	
	private void addCalendar(){
		calendarBeg = new FXCalendar();
		calendarBeg.setValue(Glob.getGame().current_date);
		grid.add(calendarBeg, 2, 5);
		calendarEnd = new FXCalendar();
        calendarEnd.setValue(Glob.getGame().current_date);
		grid.add(calendarEnd, 2, 6);
	}
	
	@FXML
	private void okClick(){
        try {
            if(!selectedCow.getValue().equals("") && ((SimpleDirItem)supCombo.getValue()) != null && Double.parseDouble(quantityTf.getText()) > 0.0) {
                
                if(calendarBeg.getValue().getTime() <= calendarEnd.getValue().getTime()) {
                    
                    returnedValue.set( new FeedPersonalRow(selectedCow.getValue(), isGroupCb.isSelected(), 
                    (SimpleDirItem)supCombo.getValue(), Double.parseDouble( quantityTf.getText()) , 
                    calendarBeg.getValue(), calendarEnd.getValue()));
                    dialogWindow.getChildren().clear();
                    Glob.dialogPane.toBack();
                    
                }

            }
        } catch(Exception ex) {
            //
        }
		
	}
}
